<?php
/**
* @file Packagem.php
* @synopsis  资源包
* @author Yee, <rlk002@gmail.com>
* @version 1.0
* @date 2015-03-16 11:27:09
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Packagem extends CI_Model
{
    const START_DOWNLOAD = 1; // 直接下载
    const SHARE_DOWNLOAD = 2; // 分享下载
    const INVITE_DOWNLOAD = 3; // 邀请下载
    const JFPOINTS_DOWNLOAD = 4; // 积分下载
    const MONEY_DOWNLOAD = 5; // 人民币下载

    const JFPOINTS_PAID = 201;  //积分下载
    const ALI_PAID = 101;  //支付宝支付
    const WALLET_PAID = 1;  //钱包支付

    //TODO上线前一定要记得改
    const IS_SHOW_MONEY = 0;  // 是否显示人民币  1  为显示 0 为不显示  默认显示

    function __construct()
    {
        parent::__construct();
        $this->load->model('sharem');
        $this->load->model("invitecodem");
    }

    public function get_zip_info($package_id)
    {
        $this->db->select('ch_describe,en_describe,zip_size,zip_location,addtime')->from('ml_package_phone');
        $this->db->where('is_show', 1)->where('id', $package_id);
        $query = $this->db->get()->result_array();
        return $query && $query[0] ? $query[0] : array();
    }


    //判断用户是否消费过
    public function get_download_part_log($uid, $part_id)
    {
        if (!$part_id) return 0;
        if (!$uid) return 0;
        $sql = "select count(1) as count from ml_download_part_log where uid= {$uid} and part_id={$part_id}";
        $query = $this->db->query($sql)->result_array();
        return $query[0]['count'];
    }

    public function insert_log($uid, $part_id, $device_id)
    {
        $query = array();
        $query['uid'] = $uid;
        $query['part_id'] = $part_id;
        $query['device_id'] = $device_id;
        $query['addtime'] = time();
        return $this->db->insert("ml_download_part_log", $query);
    }

    /*//同步老用户下载日志
    public function sync_download_log($uid,$download_parts)
    {
        foreach ($download_parts as $value) {
            $value_count = $this->get_download_part_log($uid,$value);
            if(!$value_count)
            {
                $this->insert_log($uid,$value);
            }
        }
        return true;
    }*/

    public function get_resource_list($user_id)
    {
        //w_log('uid is ' . $this->uid);
        //w_log('device_id is ' . $this->uniqu);
        $this->load->model("sharem");
        $mc = $this->cache->memcached;
        $key = 'get_resource_list';

        if (!$this->cache->memcached->is_supported()) {
            $result = array("code" => -3, 'tips' => "缓存未开启");
        }
        $lists = $mc->get($key);
        if (!$lists || in_array($user_id, array(78767, 3032, 4, 37846))) {
            $this->db->select('part_id')
                ->from('ml_part');
            if (!in_array($user_id, array(78767, 3032, 4, 37846))) {
                $this->db->where('is_show', 1); //修复图谱关闭状态下 也显示的bug
            }
            $this->db->order_by('sort');
            $lists = $this->db->get()->result_array();


            if (!in_array($user_id, array(78767, 3032, 4, 37846))) {
                $mc->save($key, $lists, 60 * 3);
            }
        }
        $data = array();
        //用户邀请码要缓存
        foreach ($lists as $part_id) {
            $part_info = $this->get_one_package($part_id['part_id'], $user_id);
            $data[] = $part_info;
        }

        return $data;
    }


    //同步老用户下载日志
    public function sync_download_log($uid, $download_parts)
    {
        if (is_array($download_parts)) {
            foreach ($download_parts as $value) {
                if ($value && $uid) {
                    $value_count = $this->get_download_part_log($uid, $value);
                    if (!$value_count) {
                        $this->insert_log($uid, $value,$this->unique_code);
                    }
                } else {
                    continue;
                }

            }
        }

        return true;
    }


    public function get_one_package($part_id, $down_user_id = 0)
    {
        if (!$part_id) return array();

        $cache_result = $this->get_package_staic_content($part_id, $down_user_id);

        /**********需要实时判断的内容**************/
        $result = $cache_result;

        //限时免费
        $now_time = time();
        if ($cache_result['free_limit_start_time'] && $cache_result['free_limit_end_time'] && ($cache_result['free_limit_start_time'] < $now_time && $now_time < $cache_result['free_limit_end_time'])) {
            $result["jfpoints"] = $cache_result['free_limit_jfpoints'];
            $result["cost_money"] = $cache_result['free_limit_money'];
        } else {
            $result["jfpoints"] = $cache_result['jfpoints'];
            $result["cost_money"] = $cache_result['cost_money'];
        }

        //获取用户邀请码并拼装到 share_url  未登录没有邀请码
        if ($down_user_id) {
            $invite_code = $this->invitecodem->get_one_invitecode($down_user_id);

        } else {
            $invite_code = '';
        }
        $result["share_url"] = WEB_URL . "share/part/" . $part_id . '?invite_code=' . $invite_code . '&source=part';


        //记录原本下载类型
        $result["paid_type_old"] = $cache_result["paid_type"];


        //是否在邀请活动时间内   //如果是邀请下载 需要判断邀请时间
        if ($cache_result["paid_type"] == 3
            && $cache_result['share_limit_start_time']
            && $cache_result['share_limit_end_time']
            && ($cache_result['share_limit_start_time'] < $now_time
                && $now_time < $cache_result['share_limit_end_time'])
        ) {
            $return_3 = $this->deal_type_3($down_user_id, $cache_result);
            $result = array_merge($result, $return_3);
        } elseif ($cache_result["paid_type"] == 2) //如果是分享下载
        {
            $return_2 = $this->deal_type_2($down_user_id, $cache_result);
            $result = array_merge($result, $return_2);
        } else {
            $return_4 = $this->deal_type_4($down_user_id, $cache_result);
            $result = array_merge($result, $return_4);
        }


        //分享如果没有则 设置为0  只有paid_type = 2的时候有用
        $result["share_count "] = $result["share_count "] ? $result["share_count "] : 0;

        $result['free_limit_start_time'] = $cache_result['free_limit_start_time'] ? date('Y-m-d H:i:s', $cache_result['free_limit_start_time']) : '';
        $result['free_limit_end_time'] = $cache_result['free_limit_end_time'] ? date('Y-m-d H:i:s', $cache_result['free_limit_end_time']) : '';

        $result["share_limit_start_time"] = $cache_result['share_limit_start_time'] ? date('Y-m-d H:i:s', $cache_result['share_limit_start_time']) : '';
        $result["share_limit_end_time"] = $cache_result['share_limit_end_time'] ? date('Y-m-d H:i:s', $cache_result['share_limit_end_time']) : '';
        //用于下载判断是否在邀请分享时间段内
        $result["share_limit_start_time_unix"] = $cache_result['share_limit_start_time'];
        $result["share_limit_end_time_unix"] = $cache_result['share_limit_end_time'];
        //if($result["paid_type"] == self::INVITE_DOWNLOAD)
        //{
        $result["active_notice"] = '注:好友认证时间需在' . date('Y.m.d', $cache_result['share_limit_start_time']) . '-' . date('Y.m.d', $cache_result['share_limit_end_time']);
        //}

        return $result;

    }

    /**
     * 获取图谱静态内容  默认为0 就是获取图谱本身内容
     * @param $part_id
     * @param $down_user_id
     * @return array
     */
    public function get_package_staic_content($part_id, $down_user_id  = 0)
    {
        if (!$part_id) return array();
        $mc = $this->mc;
        $key = 'get_one_package_new_' . $part_id;

        $cache_result = $mc->get($key);

        if (!in_array($down_user_id, array(78767, 3032, 4, 37846))) {
            $where = array('a.is_show' => 1, "a.part_id" => $part_id);
        } else {
            //测试帐号 不取缓存  并且可以看到隐藏图谱
            $where = array("a.part_id" => $part_id);
            $cache_result = [];
        }
        if (!$cache_result) {
            ci_load_once('tradem');
            $val = $this->db->select("a.*,b.cutfun_name AS cutfun_name,c.version,c.zip_location,c.zip_size,c.is_share,c.ch_describe,c.addtime as package_addtime")
                ->from('ml_part as a')
                ->join('ml_cutfun as b', 'a.parent_id = b.cutfun_id', 'left')
                ->join('(select * from `ml_package_phone` where is_show=1 order by id desc) as c', 'a.part_id = c.part_id', 'left')
                ->where($where)
                ->group_by('c.part_id')
                ->get()->row_array();
            //没有内容的时候要返回空
            if (!$val) {
                return array();
            }
            $cache_result["atlas_id"] = $val['part_id'];
            if ($val['cutfun_name'] == "其它") {
                $cache_result["atlas_name"] = $val['ch_part_name'];
                $cache_result["atlas_keyword"] = $val['source_dir'];
            } else {
                $cache_result["atlas_name"] = $val['ch_part_name'] . ' ' . $val['cutfun_name'];
                $cache_result["atlas_keyword"] = $val['source_dir'];
            }
            $cache_result["atlas_des"] = $val['ch_describe'];
            $cache_result["atlas_version"] = $val['version'];
            $cache_result["is_share"] = $val['is_share'];


            $cache_result["atlas_abstract"] = $val['abstract'] ? $val['abstract'] : "";
            $cache_result["atlas_new_content"] = $val['new_content'] ? $val['new_content'] : "";
            $cache_result["prgma_key"] = $val['prgma_key'];
            $cache_result["atlas_icon"] = ret_fullpath_url($val['images_icon']);
            $cache_result["atlas_cover"] = ret_fullpath_url($val['images_thumb']);
            $cache_result["atlas_size"] = $val['zip_size'];
            $cache_result["atlas_addtime"] = $val['package_addtime'] ? date("Y-m-d", $val['package_addtime']) : '';
            $imgs_preview = explode(",", rtrim($val['game_imgs'], ','));
            foreach ($imgs_preview as $key_img => $val_img) {
                $imgs_preview[$key_img] = ret_fullpath_url($val_img);
            }
            $cache_result["imgs_preview"] = $imgs_preview;
            $cache_result["download_url"] = STATICE_DOWNLOAD_URL . 'med_package_jpg/' . $val['zip_location'];


            $cache_result["share_desc"] = $val['share_desc'];

            $cache_result["price"] = $val['price'] ? $val['price'] : 0;


            $cache_result["atlas_size"] = $val["zip_size"];
            $cache_result["atlas_down_level"] = $val['down_level'];

            $cache_result['free_limit_start_time'] = $val['free_limit_start_time'];
            $cache_result['free_limit_end_time'] = $val['free_limit_end_time'];
            $cache_result['free_limit_jfpoints'] = $val['free_limit_jfpoints'];
            $cache_result['free_limit_money'] = $this->tradem->convertFen2Yuan($val['free_limit_money']);
            $cache_result['jfpoints'] = $val['jfpoints'];
            $cache_result['cost_money'] = $this->tradem->convertFen2Yuan($val['cost_money']);

            $cache_result["paid_type"] = $val['paid_type'];
            $cache_result["active_info"] = $val['active_info']; //邀请活动描述
            $cache_result["share_limit_start_time"] = $val['share_limit_start_time'];
            $cache_result["share_limit_end_time"] = $val['share_limit_end_time'];

            //测试帐号 不取缓存  并且可以看到隐藏图谱  也不能修改缓存
            if (!in_array($down_user_id, array(78767, 3032, 4, 37846))) {
                $mc->save($key, $cache_result, 60 * 3);
            }
        }
        return $cache_result;
    }

    /**
     * 邀请活动消耗的人数
     * @param $uid
     * @param $start_time
     * @param $end_time
     */
    public function down_cost_invite_num($uid, $start_time, $end_time)
    {
        $where = [];
        $where['uid'] = $uid;
        $where['addtime >= '] = $start_time;
        $where['addtime < '] = $end_time;
        $cost_invite_num = $this->db->select('count(id) as sum')
            ->from('ml_down_cost_invite')
            ->where($where)
            ->get()->row_array();
        //echo 'cost_sql is '.$this->db->last_query().'<br /><br />';
        return $cost_invite_num['sum'];
    }

    public function insert_cost_invite_num($uid, $part_id)
    {
        $query = array();
        $query['uid'] = $uid;
        $query['part_id'] = $part_id;
        $query['addtime'] = time();
        return $this->db->insert("ml_down_cost_invite", $query);
    }

    /**
     * 图谱是否付款成功(包括人民币和积分)  超限后需要加上设备号
     * @param $part_id
     * @param $user_id
     * @param $device_id
     * @return bool
     */
    public function get_part_ispaid($part_id, $user_id, $device_id = '')
    {

        $where_data = [];
        $where_data['part_id'] = $part_id;
        $where_data['uid'] = $user_id;
        if($device_id)
        {
            $where_data['device_id'] = $device_id;
        }

        $result = $this->db->where($where_data)->get('ml_part_order')->row_array();
        //w_log('get_part_ispaid is '.$this->db->last_query());
        if ($result) {
            $return = ['code' => true, 'pay_method' => $result['cost_type'], 'cost' => $result['cost']];
        } else {
            $return = ['code' => false, 'pay_method' => 0, 'cost' => 0];
        }
        return $return;

    }

    /**
     * 用户是否获取过这个图谱   这时不判断设备
     * @param $part_id
     * @param $user_id
     * @return array
     */
    public function get_part_got($part_id, $user_id)
    {
        $where_data = [];
        $where_data['part_id'] = $part_id;
        $where_data['uid'] = $user_id;

        $result = $this->db->where($where_data)->get('ml_part_order')->row_array();
        //w_log('get_part_ispaid is '.$this->db->last_query());
        if ($result) {
            $return = ['code' => true, 'pay_method' => $result['cost_type'], 'cost' => $result['cost']];
        } else {
            $return = ['code' => false, 'pay_method' => 0, 'cost' => 0];
        }
        return $return;

    }



    /**
     * 增加图谱订单记录
     * @param $uid
     * @param $part_id
     * @param $paid_type
     * @param $cost
     * @return object
     */
    public function insert_part_order($uid, $part_id, $paid_type, $device_id, $cost_type, $cost = 0, $trade_id = '')
    {
        $repeat_paid = 0;
        if(in_array($paid_type,array(4,5)))
        {
            $paid_count = $this->get_paid_count($uid,$part_id);
//            w_log($this->db->last_query());
//            w_log($paid_count);
            $repeat_paid = $paid_count ? 1 : 0;
        }


        $query = array();
        $query['uid'] = $uid;
        $query['part_id'] = $part_id;
        $query['paid_type'] = $paid_type;
        $query['device_id'] = empty($device_id) ? '' : $device_id;
        $query['cost_type'] = $cost_type;
        $query['cost'] = $cost;
        $query['trade_id'] = $trade_id;
        $query['repeat_paid'] = $repeat_paid;
        $query['addtime'] = time();
        return $this->db->insert("ml_part_order", $query);
    }


    public function deal_type_2($down_user_id, $cache_result)
    {
        $download_part_count = $this->get_download_part_log($down_user_id, $cache_result['atlas_id']) ? "1" : "0";
        $result["download_part_count"] = $download_part_count;


        //需要下载的话  可以直接下载的话 返回1
        if (!$download_part_count) {
            //获取分享次数  如果分享过  则直接下载
            $share_count = $this->sharem->share_all_count($down_user_id, Sharem::RESOUCE_TYPE_PART, $cache_result['atlas_id']);

            $result["share_count "] = $share_count;
            if ($share_count) {
                $result["paid_type"] = self::START_DOWNLOAD;
            } else {
                $result["paid_type"] = self::SHARE_DOWNLOAD;
                $result["jfpoints"] = 0; //如果是分享下载  则积分需要设置为0   兼容旧版本
            }
        } else {
            $result["paid_type"] = self::START_DOWNLOAD;  //以前为0 的图谱 这里设置为积分下载
        }
        return $result;
    }

    public function deal_type_3($down_user_id, $cache_result)
    {
        $download_part_count = $this->get_download_part_log($down_user_id, $cache_result['atlas_id']) ? "1" : "0";
        $result["download_part_count"] = $download_part_count;


        //需要下载的话  可以直接下载的话 返回1
        if (!$download_part_count) {
            //如果需要邀请 则判断活动期间邀请的人数 跟消耗的人数 做减法 看看是否可以下载
            $invite_num = $this->invitecodem->get_invite_count($down_user_id, $cache_result['share_limit_start_time'], $cache_result['share_limit_end_time']);
            $cost_invite_num = $this->down_cost_invite_num($down_user_id, $cache_result['share_limit_start_time'], $cache_result['share_limit_end_time']);
            //w_log('invite_num is '.$invite_num.' cost_invite_num is '.$cost_invite_num);

            if (($invite_num - $cost_invite_num) > 0) {
                $result["paid_type"] = self::START_DOWNLOAD;
            } else {
                $result["paid_type"] = self::INVITE_DOWNLOAD;
                //$result["jfpoints"] = 0; //如果是活动下载  则积分 不能为0   兼容旧版本
            }
        } else {
            $result["paid_type"] = self::START_DOWNLOAD;  //以前为0 的图谱 这里设置为积分下载
        }
        return $result;

    }

    /**
     * 需要付费的类型  首先判断设备数  判断下载数和 记录数 交易记录数
     * @param $down_user_id
     * @param $part_id
     */
    public function deal_type_4($down_user_id, $cache_result)
    {
        $device_id = $this->unique_code;
        $device_array = $this->packagem->get_download_device($down_user_id);
        //w_log(json_encode($device_array));
        $device_count = count($device_array);
        $result["paid_type"] = self::JFPOINTS_DOWNLOAD;
        if ($device_count == 2 && !in_array($device_id, $device_array)) {
            $download_result = $this->packagem->get_part_ispaid($cache_result['atlas_id'], $down_user_id,$device_id);

            if ($download_result['code'] === true) {
                $result["paid_type"] = self::START_DOWNLOAD;
            }
            //w_log('如果设备超限 并且不在前2个设备内 设备号:' . $device_id);
            //如果设备超限 并且不在前2个设备内
        } else {
            //w_log('没有设备超限 设备号:' . $device_id);
            //这里需要把 下载日志里的用户同步过来
            $download_result = $this->packagem->get_part_ispaid($cache_result['atlas_id'], $down_user_id);
            //w_log(json_encode($download_result));
            //w_log(json_encode($download_result));
            if ($download_result['code'] === true) {
                $result["paid_type"] = self::START_DOWNLOAD;
            }
        }
        $result["active_info"] = $cache_result["active_info"] ? $cache_result["active_info"] : (self::IS_SHOW_MONEY ? $cache_result["jfpoints"] . '积分/' . $cache_result["cost_money"] . '元' : $cache_result["jfpoints"] . '积分');

        return $result;
    }

    //获取用户下载记录里的前2条  老用户 设备id 为空
    public function get_download_device($down_user_id)
    {
        $return = [];
        $result = $this->db->select('id,device_id')
            ->where(array('uid' => $down_user_id, 'device_id != ' => ''))
            ->group_by('device_id')
            ->order_by('id','asc')
            ->get('ml_part_order')
            ->result_array();
        //w_log('获取用户下载记录里的前2条 '.$this->db->last_query());
        if ($result) {

            $return = count($result) > 2 ? array($result[0]['device_id'],$result[1]['device_id']) : $result;
        }

        return $return;
    }

    //购买过程 分类处理  图谱详情已经处理过是否已经下载过的问题 这里不再处理
    public function deal_cost_type_3($uid,$package_info)
    {
        //w_log('邀请活动中 $uid is '.$uid.' part_id is '.$package_info['atlas_id']);
        //如果需要邀请 则判断活动期间邀请的人数 跟消耗的人数 做减法 看看是否可以下载
        $invite_num = $this->invitecodem->get_invite_count($uid,$package_info['share_limit_start_time_unix'],$package_info['share_limit_end_time_unix']);
        $cost_invite_num = $this->packagem->down_cost_invite_num($uid,$package_info['share_limit_start_time_unix'],$package_info['share_limit_end_time_unix']);
        //w_log('$invite_num is '.$invite_num .' $cost_invite_num is '. $cost_invite_num);
        if (($invite_num - $cost_invite_num) > 0)
        {
            //w_log('down_load_type is 邀请活动  part_id:'.$package_info['atlas_id'].' uid:'.$uid);
            $insert_cost_invite_reuslt =  $this->packagem->insert_cost_invite_num($uid,$package_info['atlas_id']);
            //w_log('增加消耗 人数'.$insert_cost_invite_reuslt);
            $part_download_result = $insert_cost_invite_reuslt ? true : false;
            if ($part_download_result === true)
            {
                $cost_type = 0;
                $cost = 0;
                $return = array(
                    'code' => 1,
                    'message'  => 'success',
                    'response' => array('$paid_type'=>self::INVITE_DOWNLOAD,'cost_type'=>$cost_type,'cost'=>$cost)
                );
            }
            else
            {
                $return = array(
                    'code' => 0,
                    'message'  => '数据库错误',
                    'response' => []
                );
            }
            //w_log('是否下载 $part_download_result is '.$part_download_result);
        }
        else
        {
            $return = array(
                'code' => 0,
                'message'  => '邀请人数不足',
                'response' => []
            );
        }
        return $return;
    }

    //购买过程 分类处理  图谱详情已经处理过是否已经下载过的问题 这里不再处理
    public function deal_cost_type_2($uid,$package_info)
    {
        //w_log('分享活动中 $uid is '.$uid.' part_id is '.$package_info['atlas_id']);
        //获取分享次数  如果分享过  则直接下载
        $share_count = $this->sharem->share_all_count($uid, Sharem::RESOUCE_TYPE_PART, $package_info['atlas_id']);

        $result["share_count "] = $share_count;
        if ($share_count) {
            $return = array(
                'code' => 0,
                'message'  => '请分享,然后下载',
                'response' => []
            );
        } else {
            $return = array(
                'code' => 1,
                'message'  => 'success',
                'response' => array('paid_type'=>self::SHARE_DOWNLOAD,'cost_type'=>0,'cost'=>0)
            );
        }

        return $return;
    }

    //购买过程 分类处理  图谱详情已经处理过是否已经下载过的问题 这里不再处理
    public function deal_cost_type_4($uid,$package_info)
    {
        $device_id = $this->unique_code;
        //w_log('需要用积分 $uid is '.$uid.' part_id is '.$package_info['atlas_id']);
        $get_part_ispaid = 0;
        $device_array = $this->packagem->get_download_device($uid);
        w_log('uid is '.$uid.'   part_id is '.$package_info['atlas_id']);
        w_log('$device_array is '.json_encode($device_array));
        $device_count = count($device_array);
        if ($device_count == 2
            && !in_array($device_id, $device_array)
        ) {
            w_log('如果设备超限 并且不在前2个设备内 带设备号:' . $device_id);
            $download_result = $this->packagem->get_part_ispaid($package_info['atlas_id'], $uid,$device_id);
            //w_log('带设备号返回结果 '.json_encode($download_result));
            if ($download_result['code'] === true) {
                $return = array(
                    'code' => 1,
                    'message'  => 'success',
                    'response' => array('paid_type'=>self::MONEY_DOWNLOAD,'cost_type'=>$download_result['pay_method'],'cost'=>$download_result['cost'])
                );
                $get_part_ispaid = 1;
            }

            //w_log('如果没有付费');
            //如果没有付费
            if($get_part_ispaid == 0)
            {
                $download_result = $this->jfpointsm->part_download($uid,$package_info['atlas_id']);
                if($download_result['code'] > 0)
                {
                    $return = array(
                        'code' => 1,
                        'message'  => 'success',
                        'response' => array('paid_type'=>self::JFPOINTS_DOWNLOAD,'cost_type'=>self::JFPOINTS_PAID,'cost'=>$download_result['cost'])
                    );
                }
                else
                {
                    $return = array(
                        'code' => 0,
                        'message'  => $download_result['tips'],
                        'response' => []
                    );
                }

            }
        }
        else
        {
            w_log('如果设备不超限 或者在前2个设备内 设备号:' . $device_id);
            if($package_info['paid_type'] == 1)
            {
                $return = array(
                    'code' => 1,
                    'message'  => 'success',
                    'response' => array('paid_type'=>self::START_DOWNLOAD,'cost_type'=>0,'cost'=>0)
                );
            }
            else
            {
                $download_result = $this->packagem->get_part_ispaid($package_info['atlas_id'], $uid);
                //w_log('不带设备号返回结果 '.json_encode($download_result));
                if ($download_result['code'] === true) {
                    $return = array(
                        'code' => 1,
                        'message'  => 'success',
                        'response' => array('paid_type'=>self::MONEY_DOWNLOAD,'cost_type'=>$download_result['pay_method'],'cost'=>$download_result['cost'])
                    );
                    $get_part_ispaid = 1;
                }

                //w_log('如果没有付费');
                //如果没有付费
                if($get_part_ispaid == 0)
                {
                    $download_result = $this->jfpointsm->part_download($uid,$package_info['atlas_id']);
                    if($download_result['code'] > 0)
                    {
                        $return = array(
                            'code' => 1,
                            'message'  => 'success',
                            'response' => array('paid_type'=>self::JFPOINTS_DOWNLOAD,'cost_type'=>self::JFPOINTS_PAID,'cost'=>$download_result['cost'])
                        );
                    }
                    else
                    {
                        $return = array(
                            'code' => 0,
                            'message'  => $download_result['tips'],
                            'response' => []
                        );
                    }

                }
            }
        }

        return $return;
    }

    //获取一个图谱 个人付费购买次数
    public function get_paid_count($uid,$part_id)
    {
        $where = array(
            'uid'       =>$uid,
            'part_id'   =>$part_id,
        );
        $this->db->select('count(id) as sum')
            ->from('ml_part_order')
            ->where($where)
            ->where_in('paid_type',array(4,5));


        $total = $this->db->get()->row_array();
        return $total['sum'];
    }



    /****** 类结尾了 *******/

}